package homegroup.inc.utils;

import homegroup.inc.exceptions.IllegalBookCoverFileExtension;
import homegroup.inc.exceptions.IllegalBookFileExtension;
import homegroup.inc.exceptions.IllegalImageResolution;

import javax.swing.*;
import java.util.ArrayList;

/**
 * Created by stone on 23.11.2014.
 */
public class BookParameters {
    private ArrayList<String> permittedBookExtension;
    private ArrayList<String> permittedBookCoverEtensions;
    private static BookParameters self;
    private int coverWidth = 800;
    private int coverHeight = 600;
    private int mimeType;

    public void getMimeType() {

    }

    private BookParameters() {
        createPermitedBookExtension();
        createPermittedBookCoverExtension();

    }
    public static BookParameters getInstance() {
        if(self != null) {
            return self;
        }
        return self = new BookParameters();
    }

    private void createPermitedBookExtension() {
        permittedBookExtension = new ArrayList<>();
        permittedBookExtension.add("txt");
        permittedBookExtension.add("rtf");
        permittedBookExtension.add("doc");
        permittedBookExtension.add("odt");
        permittedBookExtension.add("pdf");
        permittedBookExtension.add("djvu");
    }

    private void setPermittedBookExtensions(ArrayList<String> extensions) {
        permittedBookExtension.addAll(extensions);
    }

    private void setPermittedBookExtension(String extension) {
        permittedBookExtension.add(extension);
    }

    private void createPermittedBookCoverExtension() {
        permittedBookCoverEtensions = new ArrayList<>();
        permittedBookCoverEtensions.add("png");
        permittedBookCoverEtensions.add("jpg");
        permittedBookCoverEtensions.add("gif");
    }

    private void setPermittedBookCoverExtensions(ArrayList<String> extensions) {
        permittedBookExtension.addAll(extensions);
    }

    public ArrayList<String> getPermittedBookExtension() {
        return permittedBookExtension;
    }

    public ArrayList<String> getPermittedBookCoverEtensions() {
        return  permittedBookCoverEtensions;
    }


    private void setPermittedBookCoverExtension(String extension) {
        permittedBookExtension.add(extension);
    }

    private void setCoverWidth(int width) {
        coverWidth = width;
    }

    public int getCoverWidth() {
        return coverWidth;
    }

    private void setCoverHeight(int height) {
        coverHeight = height;
    }

    public int getCoverHeight() {
        return coverHeight;
    }

    public boolean checkBookExtension(String extension) throws IllegalBookFileExtension {
        if (!extension.equals("")) {
            for (String permittedExtension : permittedBookExtension) {
                if (extension.equalsIgnoreCase(permittedExtension)) {
                    return true;
                }
            }
            throw new IllegalBookFileExtension();
        }
        return false;
    }

    public boolean checkBookCoverExtension(String extension) throws IllegalBookCoverFileExtension {
        if (!extension.equals("")) {
            for (String permittedExtension : permittedBookCoverEtensions) {
                if (extension.equalsIgnoreCase(permittedExtension)) {
                    return true;
                }
            }
            throw new IllegalBookCoverFileExtension();
        }
        return false;
    }

    public boolean checkCoverResolution(byte[] imageBytes) {
        ImageIcon image = new ImageIcon(imageBytes);
        if(!(image.getIconWidth() <= coverWidth &&
                image.getIconHeight() <= coverHeight)) {
            return false;
        }
        return true;
    }
}
